﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.produceDAL.Stock;
using dhDataAcess.dataAcess.stockDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.produceLinq.StockLinq;

namespace dhSystemBLL.produceBLL
{
   public  class productOrderStockListBLL
    {
        private productOrderStockListTableAdapter _productOrderStockListAdapter = null;
        private productOrderStockListTableAdapter productOrderStockListAdapter
        {
            get
            {
                if (_productOrderStockListAdapter == null) _productOrderStockListAdapter = new productOrderStockListTableAdapter();
                return _productOrderStockListAdapter;

            }
        }

        /// <summary>
        /// 查询成品库存表
        /// </summary>
        /// <returns></returns>

        public stockDataSet.productOrderStockListDataTable getproductOrderStockListAll()
        {
            try
            {
                return productOrderStockListAdapter.GetDataproductOrderStockListAll();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 根据产品名称查询成品库存表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public stockDataSet.productOrderStockListDataTable getproductOrderStockListAllBymatNum(string pname)
        {
            try
            {
                return productOrderStockListAdapter.GetDataproductOrderStockListBypname(pname);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 插入信息
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

        public int productOrderStockListInsert(productOrderStockListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productOrderStockListDAL productOrderStockList = new productOrderStockListDAL (tn ,conn );
                productOrderStockList.productOrderStockListInsert (row);
                tn.Commit();
                return i;
            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个生产单号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

        public int productOrderStockListUpdate(productOrderStockListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productOrderStockListDAL productOrderStockList = new productOrderStockListDAL(tn, conn);
                productOrderStockList.productOrderStockListUpdate(row);
                tn.Commit();
                tn.Commit();
                return i;

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="itemNum"></param>
        /// <returns></returns>

        public int productOrderStockListDel(string recordID)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productOrderStockListDAL productOrderStockList = new productOrderStockListDAL(tn, conn);
                productOrderStockList.productOrderStockListDel(recordID);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
